﻿@{
    ViewBag.Title = "数据表管理";
}
@section header{

}
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary" onclick="btn_add()"><i class="fa fa-plus"></i>新建</a>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>

@section scripts{
    <script>
        var baseUrl = "/Sys/DatabaseTable/";
        $(function () {
            gridList();
        })
        function gridList() {
            var $gridList = $("#gridList");
            $gridList.dataGrid({
                url: baseUrl + "GetList",
                datatype: "json",
                height: $(window).height() - 120,
                mtype: "POST",
                colModel: [
                    { label: "表名", name: "TableName", width: 300, align: "left" },
                    {
                        label: "记录数", name: "SumRows", width: 100, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue + "条";
                        }
                    },
                    { label: "使用大小", name: "Reserved", width: 100, align: "center" },
                    { label: "索引使用大小", name: "IndexSize", width: 120, align: "center" },
                    { label: "说明", name: "TableNameRemark", width: 200, align: "left" }
                ],
                loadComplete: function () {
                    $('.ui-jqgrid-bdiv').scrollTop(0);
                    setTimeout(function () { btn_tc(); });
                },
                subGrid: true,
                subGridOptions: {
                    "plusicon": "glyphicon-plus",
                    "minusicon": "glyphicon-minus",
                    "openicon": "ui-icon-arrowreturn-1-e",
                    "expandOnLoad": true
                },
                subGridRowExpanded: function (subgrid_id, row_id) {
                    var subgrid_table_id, pager_id;
                    var TableName = $("#gridList").jqGrid('getRowData', row_id).TableName;
                    subgrid_table_id = subgrid_id + "_t";
                    pager_id = "p_" + subgrid_table_id;
                    $("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' class='scroll'></table><div id='" + pager_id + "' class='scroll'></div>");
                    jQuery("#" + subgrid_table_id).jqGrid({
                        url: baseUrl +"GetFieldList",
                        postData: { tableName: TableName },
                        datatype: "json",
                        mtype: 'POST',
                        height: '100%',
                        rownumbers: true,
                        colModel: [
                            { label: "列名", name: "ColumnName", width: 200, align: "left" },
                            { label: "数据类型", name: "ColumnType", width: 120, align: "center" },
                            { label: "长度", name: "MaxLength", width: 57, align: "center" },
                            {
                                label: "允许空", name: "IsNullable", width: 50, align: "center",
                                formatter: function (cellvalue) {
                                    return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                                }
                            },
                            { label: "标识", name: "IsIdentity", width: 58, align: "center" },
                            {
                                label: "主键", name: "IsPrimaryKey", width: 50, align: "center",
                                formatter: function (cellvalue) {
                                    return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                                }
                            },
                            { label: "默认值", name: "DefaultValue", width: 120, align: "center" },
                            { label: "说明", name: "ColumnRemark", width: 200, align: "left" }
                        ]
                    });
                    jQuery("#" + subgrid_table_id).jqGrid('navGrid', "#" + pager_id, {
                        edit: false, add: false, del: false, refresh: false,
                        search: false
                    });
                }
            });
            $("#btn_search").click(function () {
                $("#gridList").jqGrid('setGridParam', {
                    url: baseUrl + "GetList",
                    postData: {
                        tableName: $("#txt_keyword").val().trim()
                    },
                }).setGridParam({ datatype: 'json' }).trigger('reloadGrid');
            });
        }
    </script>
}